package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.Result;
import com.itheima.reggie.dto.OrdersDto;
import com.itheima.reggie.pojo.OrderDetail;
import com.itheima.reggie.pojo.Orders;
import com.itheima.reggie.pojo.ShoppingCart;
import com.itheima.reggie.pojo.User;
import com.itheima.reggie.service.OrderDetailService;
import com.itheima.reggie.service.OrderService;
import com.itheima.reggie.service.ShoppingCartService;
import com.itheima.reggie.service.UserService;
import com.sun.org.apache.xpath.internal.operations.Or;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;
import wiremock.com.github.jknack.handlebars.Lambda;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private UserService userService;
    @Autowired
    private OrderDetailService orderDetailService;
    @Autowired
    private ShoppingCartService shoppingCartService;

    @PostMapping("/submit")
    public Result<String> submit(@RequestBody Orders order){
        log.info("传入数据：{}",order);
        orderService.submit(order);
        return Result.success("下单成功");
    }


//    PC端分页查询
    @GetMapping("/page")
    public Result<Page> page(Integer page, Integer pageSize, String number,
                             @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime beginTime,
                             @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime){
        Page<Orders> pageInfo=new Page<>(page,pageSize);
        LambdaQueryWrapper<Orders> lqw=new LambdaQueryWrapper<>();
        lqw.orderByDesc(Orders::getOrderTime);
        lqw.like(number!=null,Orders::getNumber,number);
        lqw.ge(beginTime!=null,Orders::getOrderTime,beginTime);
        lqw.le(endTime!=null,Orders::getOrderTime,endTime);
        orderService.page(pageInfo,lqw);

        Page<OrdersDto> DtoPage=new Page<>();
        BeanUtils.copyProperties(pageInfo,DtoPage,"records");
        List<Orders> records = pageInfo.getRecords();
        List<OrdersDto> list=new ArrayList<>();
        for (Orders record : records) {
            OrdersDto ordersDto=new OrdersDto();
            BeanUtils.copyProperties(record,ordersDto);
            LambdaQueryWrapper<User> lqw1=new LambdaQueryWrapper<>();
            lqw1.eq(User::getId,record.getUserId());
            User user = userService.getOne(lqw1);
            ordersDto.setUserName(user.getName());
            list.add(ordersDto);
        }
        DtoPage.setRecords(list);
        return Result.success(DtoPage);
    }
//    修改配送状态
    @PutMapping
    public Result<String> update(@RequestBody Orders orders){
        orderService.updateById(orders);
        return Result.success("商品已派送");
    }
//    手机端订单分页查询
    @GetMapping("/userPage")
    public Result<Page> userPage(Integer page,Integer pageSize){
        Page<Orders> pageInfo=new Page<>(page,pageSize);
        LambdaQueryWrapper<Orders> lqw=new LambdaQueryWrapper<>();
        lqw.orderByDesc(Orders::getOrderTime);
        lqw.eq(Orders::getUserId, BaseContext.getCurrentId());
        orderService.page(pageInfo,lqw);
        Page<OrdersDto> DtoPage=new Page<>();
        BeanUtils.copyProperties(pageInfo,DtoPage,"records");
        List<Orders> records = pageInfo.getRecords();
        List<OrdersDto> list=new ArrayList<>();
        for (Orders record : records) {
            OrdersDto ordersDto = new OrdersDto();
            BeanUtils.copyProperties(record,ordersDto);
            LambdaQueryWrapper<OrderDetail> lqw1=new LambdaQueryWrapper<>();
            lqw1.eq(OrderDetail::getOrderId,record.getId());
            List<OrderDetail> list1 = orderDetailService.list(lqw1);
            ordersDto.setOrderDetails(list1);
            list.add(ordersDto);
        }
        DtoPage.setRecords(list);
        return Result.success(DtoPage);
    }

    //    再来一单
    @PostMapping("/again")
    public Result<String> again(@RequestBody Orders orders){
        LambdaQueryWrapper<OrderDetail> lqw=new LambdaQueryWrapper<>();
        lqw.eq(OrderDetail::getOrderId,orders.getId());
        List<OrderDetail> list = orderDetailService.list(lqw);
        for (OrderDetail orderDetail : list) {
            ShoppingCart shoppingCart=new ShoppingCart();
            BeanUtils.copyProperties(orderDetail,shoppingCart);
            shoppingCart.setUserId(BaseContext.getCurrentId());
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
        }
        return Result.success("操作成功");
    }
}
